#include "game.h"
#include "display.c"

void game_zone_init()
{
    unsigned char i;
    for (i = 0; i < 19; i++)
    {
        GAME_ZONE[i] = GAME_ZONE_INIT_DATA[i];
    }
}

void game_zone_display()
{
    unsigned char i, j;
    bit top;
    bit bottom;
    unsigned int temp;
    unsigned char display_byte[] = {0, 0, 0, 0};

    // 16行数据，每2行数据同时写入，一共做8次
    // i刚好是页数
    for (i = 0; i < 8; i++)
    {
        // 每行10列，1列显示4byte数据
        for (j = 0; j < 10; j++)
        {
            // xx10 0000 0000 01xx
            // GAME_ZONE[i]
            temp = GAME_ZONE[i * 2];
            top = ((temp >> (12 - j)) & 0x0001) == 1;
            temp = GAME_ZONE[i * 2 + 1];
            bottom = ((temp >> (12 - j)) & 0x0001) == 1;
            fill_display_block_arr(top, bottom, display_byte);
            // 30是竖线
            lcd_display_bytes_in_page(i, 31 + 4 * j, display_byte, 4);
            // GAME_ZONE[i+1]
        }
    }
}

// 初始化game_shape
void game_init_shape()
{
    game_shape.shape_type = timer_rand() % 7;
    game_shape.shape_pos = timer_rand() % 4;
    game_shape.x = 3;
    game_shape.y = 0;
    game_shape.shape = TETRIS_SHAPES[game_shape.shape_type][game_shape.shape_pos];
    game_shape.next_type = timer_rand() % 7;
    game_shape.next_pos = timer_rand() % 4;
}

void game_zone_add_shape()
{
    // xxxx xxxx xxxx xxxx
    // xxxx
    // xxxx
    // xxxx
    // xxxx

    // 根据game_shpe的y坐标，修改game_zone[y~y+4] 4组数据
    unsigned char i;
    unsigned int shape_comparing_data;
    for (i = 0; i < 4; i++)
    {
        // game_shape.shape 每次循环，取出其中的4位修改到game_zone
        // xx1??????????1xx
        //    ↑↑↑↑
        // xxxx xxxx xxxx xxxx
        // 最多从低位移动9位，用于和游戏区域的最左侧对齐
        shape_comparing_data = (game_shape.shape >> (12 - 4 * i)) & 0x000F;
        shape_comparing_data <<= (9 - game_shape.x);
        // printf("shape_comparing_data:%x\n", shape_comparing_data);
        GAME_ZONE[game_shape.y + i] |= shape_comparing_data;
        // printf("%x\n", GAME_ZONE[game_shape.y + i]);
    }
}

void game_zone_remove_shape()
{
    unsigned char i;
    unsigned int shape_comparing_data;
    for (i = 0; i < 4; i++)
    {
        shape_comparing_data = (game_shape.shape >> (12 - 4 * i)) & 0x000F;
        shape_comparing_data <<= (9 - game_shape.x);
        GAME_ZONE[game_shape.y + i] &= ~shape_comparing_data;
    }
}

// 生成下一个方块
void next_game_shape()
{
    game_shape.shape_type = game_shape.next_type;
    game_shape.shape_pos = game_shape.next_pos;
    game_shape.x = 3;
    game_shape.y = 0;
    game_shape.shape = TETRIS_SHAPES[game_shape.shape_type][game_shape.shape_pos];
    game_shape.next_type = timer_rand() % 7;
    game_shape.next_pos = timer_rand() % 4;
}

// 给出一个位置，检查当前的位置的shape能不能放置到游戏区
// 看确认游戏区的格子和方块位置上的格子有没有冲突，都是1情况
/**
 * @return 0:不能放置到当前为止，1：可以放置到当前为止
 */
bit game_check_shape_placed(unsigned char x, unsigned char y,
                            unsigned int shape)
{
    unsigned char i;
    unsigned int game_zone_val;
    unsigned int shape_comparing_data;
    // 比较游戏区y~y+3 4个数组元素，其中4位进行比较
    for (i = 0; i < 4; i++)
    {
        game_zone_val = GAME_ZONE[y + i];
        // printf("game_zone_val:%x\n", game_zone_val);
        // xx1??????????1xx
        //    ↑↑↑↑
        shape_comparing_data = ((shape >> (12 - 4 * i)) & 0x000F) << (9 - x);
        // printf("shape_comparing_data:%x\n", shape_comparing_data);
        // 与运算结果位1，说明当前位置两个都是1，说明游戏区这个位置已经有方块，不能放到这里
        if ((GAME_ZONE[y + i] & shape_comparing_data) != 0x0000)
        {
            return 0;
        }
    }
    return 1;
}